var _util = require('util/util.js');

//若用户已登录，则返回账户信息，否则返回错误信息
const getAccount=function(){
    return new Promise(function(resolve,reject){
        
        $.ajax({
            xhrFields: {
                withCredentials: true
            },
            type    : 'POST',
            url: _util.getServerURL('account/getAccountInfo'),
            dataType: "json",
            success:function(res){
                console.log("success");
                if(res.status===0){
                    console.log(res.status);
                    console.log("000000");
                    //从后端传值到前端
                    let data=res.data;
                    console.log("!!!!!!!!!!!"+data);
                    $('#username').val(data.username);
                    $('#password').val(data.password);
                    $('#phone').val(data.phone);
                    $('#firstname').val(data.firstName);
                    $('#lastname').val(data.lastName);
                    $('#email').val(data.email);
                    $('#address1').val(data.address1);
                    $('#address2').val(data.address2);
                    $('#city').val(data.city);
                    $('#zip').val(data.zip);
                    $('#state').val(data.state);
                    $('#country').val(data.country);
                    $('#languagepre').val(data.languagepre);
                }else{
                    if(res.status===10){
                        console.log("10");
                        window.location.href = './account-login.html?redirect=' + encodeURIComponent(window.location.href);
                    }else{
                        console.log("1111111111");
                        console.log(res.status);
                        alert("wrong");
                    }
                }
               
            },
            error:function(){
                console.log("获取失败");
                alert('服务器异常');
            }
        })
    })
}

getAccount();
//修改用户信息
const editAccount=function(){
    return new Promise(function(resolve,reject){
        $.ajax({
            xhrFields:{
                withCredentials:true
            },
            type:'POST',
            url: _util.getServerURL('account/update_account'),
            dataType:"json",
            data:{
                username    :   $('#username').val(),
                password    :   $('#password').val(),
                repassword  :   $('#repassword').val(),
                phone       :   $('#phone').val(),
                firstName   :   $('#firstname').val(),
                lastName    :   $('#lastname').val(),
                email       :   $('#email').val(),
                address1    :   $('#address1').val(),
                address2    :   $('#address2').val(),
                city        :   $('#city').val(),
                state       :   $('#state').val(),
                zip         :   $('#zip').val(),
                country     :   $('#country').val(),
                languagepre :   $('#languagepre').val()
            },
            success:function(res){
                console.log(res.status);
                console.log("!!!!000000");
                if(res.status===0){
                    //后端验证是否为空
                    console.log("!!!!---------000000");
                    if(res.msg=="所填项不能为空"){
                        alert("提交信息不能为空");
                        return;
                    }else{
                        alert("修改成功，请登录");
                        window.location.href = './account-login.html?redirect=' + encodeURIComponent(window.location.href);
                    }

                }
                    
            },
            error:function(){
                alert('error');

            }
        })
    })
}

$('#editbtn').on('click',editAccount);